﻿using SpiderStudy.Utils;
using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Helper;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite.Bcy
{
    internal class BcyDayScanner
    {
        /// <summary>
        /// 半次元日榜API P参数最多5，date参数格式为：20180522
        /// </summary>
        private string DayAPI = "https://bcy.net/coser/index/ajaxloadtoppost?p=&type=lastday&date={1}";

        /// <summary>
        /// 半次元Day扫描
        /// </summary>
        /// <param name="StartDays"></param>
        /// <param name="EndDays"></param>
        /// <returns></returns>
        public void DaySacanner(string StartDays, string EndDays)
        {
            List<string> TimeList = GetScannerList(StartDays, EndDays, "yyyyMMdd");
            List<string> UrlList = new List<string>();
            foreach (string Line in TimeList) {
            
                string TempAPI = string.Format(DayAPI, "{0}", Line);
                for (int i = 1; i < 6; i++)
                {
                    Console.WriteLine(string.Format(TempAPI, i));
                    string Shtml = WebHelper.HttpGet(string.Format(TempAPI, i));
                    List<string> LinkList = HtmlHelper.GetLinkVlaueList(Shtml, "//a[@class='db posr ovf']");
                    foreach (string Link in LinkList) {
                        if (UrlList.Contains("https://bcy.net" + Link))
                        {
                            Console.WriteLine("已有，不添加"+UrlList.Count);
                            continue;
                        }
                        UrlList.Add("https://bcy.net" + Link);
                    }
                    Console.WriteLine(Shtml.Length);
                }
            }
            new SaveHelper().SaveList(StaticValue.TempPath +  "Bcy"+ "\\", TextHelper.ReplaceBadChar(string.Format("半次元{0}-{1}.txt", StartDays, EndDays)), UrlList);
           
        }

   

        /// <summary>
        /// 获取时间参数列表
        /// </summary>
        /// <param name="StartDays"></param>
        /// <param name="EndDays"></param>
        /// <param name="TimeMode"></param>
        private List<string> GetScannerList(string StartDays, string EndDays, string TimeMode)
        {
            List<string> TempList = new List<string>();
            int SubDays = new TimeHelper().DisparityDay(StartDays, EndDays);
            if (SubDays < 0)
            {
                MessageHelper.Alert("起始时间不能小于结束时间");
                return TempList;
            }
            DateTime dateTime = Convert.ToDateTime(EndDays);
            for (; SubDays > 0; SubDays--)
            {
                TempList.Add(dateTime.AddDays(0 - SubDays).ToString(TimeMode));
            }
            return TempList;
        }
    }
}